<template>
  <el-card style="margin: 10px 0">
    <el-table :data="skuList" style="margin: 60px 0">
      <el-table-column
        type="index"
        width="180px"
        label="序号"
      ></el-table-column>
      <el-table-column
        prop="skuName"
        width="180px"
        label="名称"
      ></el-table-column>
      <el-table-column
        prop="skuDesc"
        width="180px"
        label="描述"
      ></el-table-column>
      <el-table-column width="180px" label="默认图片">
        <template #="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        width="180px"
        label="重量"
      ></el-table-column>
      <el-table-column
        prop="price"
        width="180px"
        label="价格"
      ></el-table-column>
      <el-table-column width="230px" fixed="right" label="操作">
        <template #="{ row }">
          <el-button
            size="small"
            @click="onORcancelSKU(row.isSale, row.id)"
            :type="row.isSale ? 'default' : 'primary'"
            :icon="row.isSale ? Bottom : Top"
          ></el-button>
          <el-button size="small" type="primary" :icon="Edit"></el-button>
          <el-button
            @click="isShowDialog(row)"
            size="small"
            type="primary"
            :icon="InfoFilled"
          ></el-button>
          <el-button
            @click="deleteSKUFun(row.id)"
            size="small"
            type="primary"
            :icon="Delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 25, 30]"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total"
    />
  </el-card>
  <!-- SKU详情抽屉 -->
  <el-drawer
    :show-close="false"
    size="50%"
    v-model="isShowDialogRef"
    title="SKU商品详情"
  >
    <!-- 自定义头部 -->
    <template #header="{ close, titleId, titleClass }">
      <h4 :id="titleId" :class="titleClass">This is a custom header!</h4>
      <el-button size="small" type="danger" @click="close">
        <el-icon class="el-icon--left"><CircleCloseFilled /></el-icon>
        关闭
      </el-button>
    </template>
    <el-divider content-position="left">商品信息</el-divider>
    <el-row style="display: flex" justify="center">
      <el-col :span="8">名称</el-col>
      <el-col :span="16">{{ SKUContentList.skuName }}</el-col>
    </el-row>
    <el-divider content-position="left">商品描述</el-divider>
    <el-row style="display: flex" justify="center">
      <el-col :span="8">描述</el-col>
      <el-col :span="16">{{ SKUContentList.skuDesc }}</el-col>
    </el-row>
    <el-divider content-position="left">商品价格</el-divider>
    <el-row style="display: flex" justify="center">
      <el-col :span="8">价格</el-col>
      <el-col :span="16">{{ SKUContentList.price }}</el-col>
    </el-row>
    <el-divider content-position="left">平台属性</el-divider>
    <el-row style="display: flex" justify="center">
      <el-col :span="8">平台属性</el-col>
      <el-col :span="16"
        ><el-tag
          v-for="item in SKUContentList.skuAttrValueList"
          :key="item.id"
          style="margin: 4px"
          >{{ item.valueName }}</el-tag
        ></el-col
      >
    </el-row>
    <el-divider content-position="left">销售属性</el-divider>
    <el-row style="display: flex" justify="center">
      <el-col :span="8">销售属性</el-col>
      <el-col :span="16"
        ><el-tag
          v-for="item in SKUContentList.skuSaleAttrValueList"
          :key="item.id"
          style="margin: 4px"
          >{{ item.saleAttrValueName }}</el-tag
        ></el-col
      >
    </el-row>
    <el-divider content-position="left">商品图片</el-divider>
    <el-row style="display: flex" justify="center">
      <el-col :span="8">商品图片</el-col>
      <el-col :span="16">
        <el-carousel :interval="4000" type="card" height="200px">
          <el-carousel-item
            v-for="item in SKUContentList.skuImageList"
            :key="item.id"
          >
            <img style="width: 100%; height: 100%" :src="item.imgUrl" alt="" />
          </el-carousel-item>
        </el-carousel>
      </el-col>
    </el-row>
  </el-drawer>
</template>

<script setup lang="ts">
import { Bottom, Delete, Edit, InfoFilled, Top } from "@element-plus/icons-vue";
import { onMounted, ref } from "vue";
import {
  getSKUAPI,
  cancelSKUAPI,
  onSKUAPI,
  getSKUContentAPI,
  delSKUAPI,
} from "@/api/product/sku";
import { ElMessage } from "element-plus";
import { CircleCloseFilled } from "@element-plus/icons-vue";
//分页器当前页
const currentPage = ref<number>(1);
//分页器每页多少条
const pageSize = ref<number>(10);
//分页器信息总条数
const total = ref<number>();

//存储SKU列表数据
const skuList = ref<any>([]);

//获取SKU列表数据
const getSKUFun = async (page = 1, size = 10) => {
  page = currentPage.value;
  size = pageSize.value;
  const res = await getSKUAPI(page, size);
  skuList.value = res.records;
  total.value = res.total;
};
//分页器每页总是改变函数
const sizeChange = (e: any) => {
  pageSize.value = e;
  getSKUFun();
};
//分页器当前页改变
const currentChange = (e: any) => {
  currentPage.value = e;
  getSKUFun();
};
//下架或者上架
const onORcancelSKU = async (isSale: number, id: number) => {
  //上架状态，点击下架
  if (isSale) {
    await cancelSKUAPI(id);
    getSKUFun();
    ElMessage({
      type: "success",
      message: "下架成功",
    });
  } else {
    ////下架状态，点击上架
    await onSKUAPI(id);
    getSKUFun();
    ElMessage({
      type: "success",
      message: "上架成功",
    });
  }
};
//SKU详情显示变量
const isShowDialogRef = ref<boolean>(false);
//存储SKU详情对象
const SKUContentList = ref<any>({});
//SKU详情
const isShowDialog = async (row: any) => {
  isShowDialogRef.value = true;
  const res = await getSKUContentAPI(row.id);
  SKUContentList.value = res;
};
//删除SKU
const deleteSKUFun = async (id: number) => {
  await delSKUAPI(id);
  getSKUFun();
  ElMessage({
    type: "success",
    message: "删除成功",
  });
};
onMounted(() => {
  getSKUFun();
});
</script>
<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
/* 深度选择器 */
/* 原生的css写法 >>>.el-carousel__item */
/* /deep/.el-carousel__item 一般less样式中写法 css也可 */
/* ::v-deep(选择器) 一般sass样式写法 css也可 */
::v-deep(.el-carousel__button) {
  width: 10px;
  height: 10px;
  background-color: yellowgreen;
  border-radius: 100%;
}
</style>
